﻿<%@ WebHandler Language="C#" Class="RbFocusContainerProcessReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using ReportBusiness;

/**
 * Description: 热表重点件号批次加工进度查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date: 2022.8.17
 **/

public class RbFocusContainerProcessReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string sql = @"
SELECT rb.factoryname,rb.productname,rb.productdesc productdescription,rb.containername,rb.qty,rb.workflowstepname,rb.currentprocess,
       rb.receivedate,rb.needfinishdate,
       NVL(rb.finishdate,t.shipindate) finishdate,
       e.fullname createuser,rb.createdate,DECODE(rb.sourcetype,1,'MES',2,'其他') sourcetype,
       DECODE(NVL(rb.finishdate,t.completedate),NULL,'未完成','已完成') status
FROM rbfocuscontainerprocess rb
LEFT JOIN r_shipboard_jijia@mesreport t ON t.containername = rb.containername AND t.workflowstepname = rb.workflowstepname
LEFT JOIN employee e ON e.employeeid = rb.createemployeeid
WHERE rb.isuse = 0 ";
        //主制分厂
        if (jObject["factoryname"] != null && jObject["factoryname"].ToString() != "")
        {
            sql += string.Format(" AND rb.factoryname = '{0}'", jObject["factoryname"].ToString());
        }

        //零件号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND rb.productname = '{0}'", jObject["productname"].ToString());
        }

        //批次号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND rb.containername = '{0}'", jObject["containername"].ToString());
        }

        //工序
        if (jObject["workflowstepname"] != null && jObject["workflowstepname"].ToString() != "")
        {
            sql += string.Format(" AND rb.workflowstepname like '%{0}%'", jObject["workflowstepname"].ToString());
        }

        //接收时间
        if (jObject["receivedate"] != null && jObject["receivedate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["receivedate"].ToString());
            if (arr.Count == 2)
            {
                sql += string.Format(" AND rb.receivedate >= TO_DATE('{0}','yyyy-mm-dd')", arr[0].ToString());
                sql += string.Format(" AND rb.receivedate <= TO_DATE('{0}','yyyy-mm-dd')", arr[1].ToString());
            }
        }

        //数据来源
        if (jObject["sourcetype"] != null && jObject["sourcetype"].ToString() != "")
        {
            sql += string.Format(" AND rb.sourcetype = {0}", jObject["sourcetype"].ToString());
        }

        //完成状态
        if (jObject["status"] != null && jObject["status"].ToString() != "")
        {
            string status = jObject["status"].ToString();
            if (status == "已完成")
            {
                sql += " AND NVL(rb.finishdate,t.completedate) IS NOT NULL";
            }
            else
            {
                sql += " AND NVL(rb.finishdate,t.completedate) IS NULL";
            }
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = getSearchSqlString(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dtResult = dal.GetDataTable(sql);
        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dtResult.Columns["factoryname"].Caption = "主制分厂";
        dtResult.Columns["productname"].Caption = "零件图号";
        dtResult.Columns["productdescription"].Caption = "零件名称";
        dtResult.Columns["containername"].Caption = "流水卡号";
        dtResult.Columns["qty"].Caption = "数量";
        dtResult.Columns["workflowstepname"].Caption = "工序";
        dtResult.Columns["currentprocess"].Caption = "加工进度";
        dtResult.Columns["receivedate"].Caption = "接收时间";
        dtResult.Columns["needfinishdate"].Caption = "要求完成时间";
        dtResult.Columns["finishdate"].Caption = "完成时间";
        dtResult.Columns["createuser"].Caption = "创建人";
        dtResult.Columns["createdate"].Caption = "创建时间";
        dtResult.Columns["sourcetype"].Caption = "数据来源";
        dtResult.Columns["status"].Caption = "完成状态";
        ExportHelper.ExportExcel(dtResult, jObject["tofactoryname"].ToString() + "重点件号批次加工进度查询报表");
    }
}